import Taro from "@tarojs/taro";

// 封装的跳转分包的路由写法
export function navigateTo(url, param) {
  console.log("url", url);
  let newUrl = url;
  if (param) {
    newUrl = `${url}?param=${JSON.stringify(param)}`;
  }
  console.log(newUrl);
  Taro.navigateTo({
    url: newUrl,
    fail(_res) {
      Taro.redirectTo({
        url: newUrl,
      });
    },
  });
}

export function getParams() {
  if(!Taro.getCurrentInstance().page.$taroParams.param){
    return false
  }
  return JSON.parse(Taro.getCurrentInstance().page.$taroParams.param);
}

export function formatTime(time) {
  const hours = Math.floor(time / 3600);
  const minutes = Math.floor((time % 3600) / 60);
  const secs = time % 60;
  return [hours, minutes, secs]
    .map(num => num < 10 ? '0' + num : num)
    .filter((num, i) => i > 0 || num !== '00')
    .join(':');
};

export function secondsToMinutes(seconds) {

  if(!seconds){
    return '0秒'
  }

  if(seconds>= 3600){
    let hours = Math.floor(seconds / 3600);
    let remainingHours = (seconds % 3600)/60;
    if(remainingHours){
      return hours+'小时'+remainingHours.toFixed(0)+'分'
    }else{
      return hours+'小时'
    }
    
  }

  // 将秒数转换成分钟，并取整
  var minutes = Math.floor(seconds / 60);
  // 获取余下的秒数
  var remainingSeconds = seconds % 60;

  // 返回分钟和秒数的对象
  if(minutes){
    return minutes+'分'+remainingSeconds+'秒'
  }else{
    return remainingSeconds+'秒'
  }
}
